//
//  MqInformationViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInformationViewController.h"
#import "MqInfoNewTableViewController.h"
#import "MqInfoWebViewController.h"
#import "MqInfoCompetitionTableViewController.h"
#import "MqInfoPictureTableViewController.h"
#import "MqInfoPictureShowViewController.h"
#import "MqInfoTopicTableViewController.h"
#import "MqInfoTopicDetailTableViewController.h"
#import "MqInfoTopicModel.h"
#import "MqInfoKnowledgeTableViewController.h"
#import "MqInfoSearchViewController.h"
@interface MqInformationViewController ()<UIScrollViewDelegate,UMSocialUIDelegate>


// 主scrollView
@property (strong, nonatomic) UIScrollView *mainScrollView;
// 主scrollView上的4个控制器
@property (strong, nonatomic) MqInfoNewTableViewController *InfoNewTC;
@property (strong, nonatomic) MqInfoCompetitionTableViewController *CompetitionTC;
@property (strong, nonatomic) MqInfoPictureTableViewController *InfoPictureTC;
@property (strong, nonatomic) MqInfoTopicTableViewController *InfoTopicTC;

@property (strong, nonatomic) MqInfoKnowledgeTableViewController *knowledgeTC;

// 频道view
@property (strong, nonatomic) UIView *channelView;

// 频道按钮
@property (strong, nonatomic) UIButton *NewButton;
@property (strong, nonatomic) UIButton *videpButon;
@property (strong, nonatomic) UIButton *picButton;
@property (strong, nonatomic) UIButton *topicButton;

// 滑动的line
@property (strong, nonatomic) UILabel *moveLabel;

// 记录之前的tag值,用来修改之前按钮的颜色
@property (assign, nonatomic) NSInteger beforeTag;







@end

@implementation MqInformationViewController


-(void)dealloc
{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"push" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"picturePush" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"topicPush" object:nil];
    
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
   // self.headBar.backgroundColor = [UIColor grayColor];
#pragma maek ----- 自定义导航栏
    [self addNavBar];
    
    
#pragma maek ----- 创建mainScrollView
    [self addMainScrollView];
    
    
}

- (void)addNavBar
{
    
    _channelView = [[UIView alloc] initWithFrame:CGRectMake(0, 20*kWidthBase,kScreenWidth * 4 / 5 , 40 * kHeightBase)];
//    _channelView.backgroundColor = [UIColor yellowColor];
    [self.headBar addSubview:_channelView];
    
    
    // 添加4个频道
    NSArray *temp = @[@"最新",@"赛事",@"图库",@"专题",@"知识"];
    for (int i = 0; i < temp.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setTitle:temp[i] forState:(UIControlStateNormal)];
        button.frame = CGRectMake(10*kWidthBase * (i+1) + i * (_channelView.width - 5*10*kWidthBase) / 5, 0, (_channelView.width - 5*10*kWidthBase) / 5, _channelView.height);
        
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(channelButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_channelView addSubview:button];
        
    }
    // 配对一下
    _NewButton = [_channelView viewWithTag:100];
    [_NewButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    _beforeTag = 100;
    _videpButon = [_channelView viewWithTag:101];
    _picButton = [_channelView viewWithTag:102];
    _topicButton = [_channelView viewWithTag:103];
    
    _moveLabel = [[UILabel alloc] initWithFrame:CGRectMake(10*kWidthBase , _channelView.bottom, (_channelView.width - 5*10*kWidthBase) / 6, 3*kHeightBase)];
    _moveLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:_moveLabel];
    
    UIView *funView = [[UIView alloc] initWithFrame:CGRectMake(_channelView.right, _channelView.top, kScreenWidth / 5, _channelView.height)];
    [self.headBar addSubview:funView];
    
    UIButton *searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    searchButton.frame = CGRectMake(10*kWidthBase, 10*kHeightBase, 20*kWidthBase, 20*kHeightBase);
    [searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:(UIControlStateNormal)];
    [searchButton addTarget:self action:@selector(searchAciton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [funView addSubview:searchButton];
    
}

#pragma mark --- 搜索按钮方法
- (void)searchAciton:(UIButton *)sender
{
    
    MqInfoSearchViewController *searchVC = [[MqInfoSearchViewController alloc] init];
    
    
    [self.navigationController pushViewController:searchVC animated:YES];
    
   
   
}


- (void)addMainScrollView
{
#pragma mark ---- 创建mainScroll
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64*kHeightBase, kScreenWidth, kScreenHeight - (64+49)*kHeightBase)];
    // 设置轮播的滑动范围
    _mainScrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
    // 设置是否回弹
    _mainScrollView.bounces = NO;
    // 关闭水平的轨迹
   // _mainScrollView.showsHorizontalScrollIndicator = YES;
    // 整页滑动
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.delegate = self;
    
    // 添加
    [self.view addSubview:_mainScrollView];
    
#pragma mark ---- 创建4个TC
    _InfoNewTC = [[MqInfoNewTableViewController alloc] init];
    _InfoNewTC.view.frame = CGRectMake(0, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_InfoNewTC.view];
 
    _CompetitionTC = [[MqInfoCompetitionTableViewController alloc] init];
    [self addChildViewController:_CompetitionTC];
    _CompetitionTC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_CompetitionTC.view];
    
    _InfoPictureTC = [[MqInfoPictureTableViewController alloc] init];
    _InfoPictureTC.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_InfoPictureTC.view];
    
    _InfoTopicTC =  [[MqInfoTopicTableViewController alloc] init];
    _InfoTopicTC.view.frame = CGRectMake(kScreenWidth * 3, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_InfoTopicTC.view];
    
    _knowledgeTC =  [[MqInfoKnowledgeTableViewController alloc] init];
    _knowledgeTC.view.frame = CGRectMake(kScreenWidth * 4, 0, kScreenWidth, _mainScrollView.height);
    [self addChildViewController:_knowledgeTC];
    [_mainScrollView addSubview:_knowledgeTC.view];

    
    // 添加通知,实现push的跳转
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:) name:@"push" object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:) name:@"picturePush" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:) name:@"topicPush" object:nil];
    
}

// 实现通知的方法
- (void)notificationAction:(NSNotification *)noti
{
    
    if ([noti.name isEqualToString:@"push"]) {
        
        NSDictionary *dic = noti.userInfo;
        NSString *ids = dic[@"ids"];
        NSString *classID = dic[@"classID"];
        MqInfoWebViewController *webView = [[MqInfoWebViewController alloc] init];
        
        webView.idsURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=%@&id=%@&userid=682480&version=1",classID, ids];
        
        // 标签的网址
        webView.tagListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getArticleTagOrTagListByType&type=tagList&id=%@",ids];
        
        // 作者信息的网址
        webView.authorInfoURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getAuthorInfByType&type=authorInfo&classid=%@&id=%@",classID,ids];
        
        // 相关文章的网址
        webView.recommendListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&version=1&platform=1&classid=%@&showType=recommendList",classID];
        // 评论的网址
        webView.commentURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=comment&methodType=getArticleCommentV1&access_token=&classid=%@&id=%@",classID,ids];
        
        
        [self.navigationController pushViewController:webView animated:YES];
        
    }
    if ([noti.name isEqualToString:@"picturePush"]) {
        
        NSDictionary *dic = noti.userInfo;
        NSString *picID = dic[@"id"];
        NSString *title = dic[@"title"];
    
         MqInfoPictureShowViewController *picShow = [[MqInfoPictureShowViewController alloc] init];
        
        picShow.picUrl = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=120&id=%@",picID];
        
        picShow.titleString = title;
        
        [self.navigationController pushViewController:picShow animated:YES];
    }
    
    if ([noti.name isEqualToString:@"topicPush"]) {
        
        NSDictionary *dic = noti.userInfo;
        NSString *ID = dic[@"id"];
        MqInfoTopicModel *model = dic[@"model"];
        MqInfoTopicDetailTableViewController *topicDetailTC = [[MqInfoTopicDetailTableViewController alloc] init];
        topicDetailTC.requestUrl = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getSpecialListByZtId&&classid=1000&&page=1&platform=1&pz=20&showType=specialList&time=0&version=1&ztid=%@",ID];
        topicDetailTC.topicModel = model;
        [self.navigationController pushViewController:topicDetailTC animated:YES];
        
    }

    
}



// 实现频道按钮的方法
- (void)channelButtonAction:(UIButton *)button
{
    
    // 修改之前按钮的颜色
    UIButton *beforeButton = (UIButton *)[_channelView viewWithTag:_beforeTag];
    [beforeButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    _beforeTag = button.tag;
    
    NSInteger index = button.tag - 100;
    
    [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
      _mainScrollView.contentOffset = CGPointMake(index * kScreenWidth, 0);
        
      _moveLabel.frame = CGRectMake(10 * kWidthBase * (index + 1) + index * (_channelView.width - 5 * 10 *kWidthBase) / 5,_moveLabel.top,(_channelView.width - 5 * 10 * kWidthBase) / 6, 3*kHeightBase);
        
    }];
    
   
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat index = scrollView.contentOffset.x / kScreenWidth;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _moveLabel.frame = CGRectMake(10 * kWidthBase * (index + 1) + index * (_channelView.width - 5 * 10 *kWidthBase) / 4,_moveLabel.top,(_channelView.width - 5 * 10 * kWidthBase) / 4, 3*kHeightBase);
        
    }];
    
    
}

// 修改按钮的颜色
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / kScreenWidth;
    
    // 修改之前按钮的颜色
    UIButton *beforeButton = (UIButton *)[_channelView viewWithTag:_beforeTag];
    [beforeButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    _beforeTag = index + 100;

    // 修改现在按钮的颜色
    UIButton *nowButton = (UIButton *)[_channelView viewWithTag:index + 100];
    [nowButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
